/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Nov 6, 2013  12:02:12 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.interfaces.controller.ApplicationLogs
 * File Name : ApplicationLogsController.java
 * TODO
 */
package com.tis.interfaces.controller.ApplicationLogs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import promis.utility.CommonService;

import com.tis.database.entitys.staging.ApplicationLoging;
import com.tis.database.services.staging.IApplicationLogingServices;
import com.tis.interfaces.controller.commons.CommonController;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Nov 6, 2013 : 12:02:12 PM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
@Controller
@RequestMapping(value=ApplicationLogsController.url)
public class ApplicationLogsController extends CommonController {

	private static final Logger logging = LoggerFactory.getLogger(ApplicationLogsController.class);
	
	public static final String url = "/application/logs/list";
	public static final String uri = "/appslogs";
	
	@RequestMapping(value="index")
	public String BacklogsIndex(HttpServletRequest request, Model model) throws Exception {
		logging.info(url + "/application/logs/list/index {Index}");
		setCurrentAppsInstanceNo();
		KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
		
		try {
			//Put History 		
			try { UserAccessTime(request, this.toString()); }catch(Exception e) {e.printStackTrace();}
			
			if(!UserLoginValidation((request)))
				return "redirect:/login";
			
		} catch (Exception e) {
			logging.error(e.getMessage());
			throw e;
		}
		 
		AppsLogsForm form = new AppsLogsForm();
		form.setMaxResult("5000");
		
		logging.info("form =" + form.toString());
		model.addAttribute("ObjectForm", form);
		return uri + "/logs_list";
	}
	
	@RequestMapping(value="search", method = RequestMethod.POST)
	public String Search(HttpServletRequest request, Model model, @ModelAttribute("ObjectForm") @Valid	AppsLogsForm form, BindingResult result) throws Exception {
		try {
			logging.info(url + "/search {search}");
			setCurrentAppsInstanceNo();
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
			
			//Put History 		
			try { UserAccessTime(request, this.toString()); }catch(Exception e) {e.printStackTrace();}
			
			if(!UserLoginValidation((request)))
				throw new Exception("Can't Search, Please Login first !!!!");
			
		} catch (Exception e) {
			e.printStackTrace();
			logging.error(e.getMessage());
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "E", CommonService.CutString(e.getMessage(), 2900), null, getCurrentAppsInstanceNo()));
			throw e;
		}
		
		return uri + "/logs_list";
	}
	
	@Autowired
	private IApplicationLogingServices iApplicationLogingServices;
	
	@RequestMapping(value="search/json", method = RequestMethod.GET, headers="accept=application/json")
	@ResponseBody
	public List<ApplicationLoging> Search(
											@RequestParam(value="uri", required=false) String uri,
											@RequestParam(value="hostName", required=false) String hostName,
											@RequestParam(value="refInstanceNo", required=false) String refInstanceNo,
											@RequestParam(value="msgCode", required=false) String msgCode,
											@RequestParam(value="msgDesc", required=false) String msgDesc,
											@RequestParam(value="msgAltDesc", required=false) String msgAltDesc,
											@RequestParam(value="instanceNo", required=false) String instanceNo,											
											@RequestParam(value="createdOnStart", required=false) String createdOnStart,
											@RequestParam(value="createdOnEnd", required=false) String createdOnEnd,
											@RequestParam(value="maxResult", required=true) String maxResult,
											HttpServletRequest request, 
											HttpServletResponse response) throws Exception {
		
		logging.info(url + "search/json {Search} ::: method = RequestMethod.GET");
		setCurrentAppsInstanceNo();
		KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
		
		if(!UserLoginValidation((request))) {
			response.setStatus(500);
			return null;
		}
		
		try {
			
			if(!UserLoginValidation((request)))
				throw new Exception("Can't Search, Please Login first !!!!");
			
			logging.info("uri --> " +  uri); 
			logging.info("hostName --> " +  hostName); 
			logging.info("refInstanceNo --> " +  refInstanceNo);
			logging.info("msgCode --> " +  msgCode);
			logging.info("msgDesc --> " +  msgDesc);
			logging.info("msgAltDesc --> " +  msgAltDesc);
			logging.info("instanceNo --> " +  instanceNo);
			logging.info("createdOnStart --> " + createdOnStart);
			logging.info("createdOnEnd --> " +  createdOnEnd);
			logging.info("maxResult --> " +  maxResult);
			
			if(CommonService.IsNull(createdOnStart))
				createdOnStart = "1980-01-01";
			
			if(CommonService.IsNull(createdOnEnd))
				createdOnEnd = "2017-12-31";

			int maxResultInt = -1;
			if(!CommonService.IsNull(maxResult))
				maxResultInt = (Integer.parseInt(CommonService.IsNullRZero(maxResult)) == 0) ? -1 : Integer.parseInt(CommonService.IsNullRZero(maxResult));
					
			List<ApplicationLoging> lst = iApplicationLogingServices.Search(null, instanceNo, msgCode, msgDesc, msgAltDesc, refInstanceNo, createdOnStart, createdOnEnd, maxResultInt);
			
			logging.info("ApplicationLoging Size = " + CommonService.getSize(lst));
			
			/*
			if(CommonService.getSize(lst) > 0){
				for(ApplicationLoging value : lst)
					logging.info(value.toString());
			}*/
			
			if(CommonService.getSize(lst) > 0) 
				return lst;
			
		} catch (Exception e) {
			e.printStackTrace();
			logging.error(e.getMessage());
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "E", CommonService.CutString(e.getMessage(), 2900), null, getCurrentAppsInstanceNo()));
			throw e;
		}
		
		return null;
	}
	
	
	@RequestMapping(value = "/exworksheet/{sessionId}")
	 public ModelAndView getWorksheet(@PathVariable String sessionId) {
		Map<String,String> revenueData = new HashMap<String,String>();
			revenueData.put("Jan-2010", "$100,000,000");
			revenueData.put("Feb-2010", "$110,000,000");
			revenueData.put("Mar-2010", "$130,000,000");
			revenueData.put("Apr-2010", "$140,000,000");
			revenueData.put("May-2010", "$200,000,000");
			return new ModelAndView("ExcelRevenueSummary","revenueData",revenueData);
	}
}
